package problem42;

class Solution3 {
    public int trap(int[] height) {
        int sum = 0;
        int left = 0, right = height.length-1;
        int maxL = height[left], maxR = height[right];
        while(left < right) {
            if(maxL < maxR) {
                left++;
                maxL = Math.max(maxL, height[left]);
                sum += (maxL - height[left]);
            }else {
                right--;
                maxR = Math.max(maxR, height[right]);
                sum += (maxR - height[right]);
            }
        }
        return sum;
    }
}